package com.gs.view.login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.gs.Controllore;
import com.gs.GSAActivity;
import com.gs.R;
import com.gs.domain.Utenti;
import com.gs.domain.wrapper.WrapUtenti;
import comi.gsi.view.MyBaseActivity;

public class Login extends MyBaseActivity {
	
	private EditText user = null;
	private EditText pass = null;
	private CheckBox utenteDefault = null;
	
	@Override
	protected int getIdLayout() {
		return R.layout.login;
	}

	@Override
	protected void myOnCreate(final Bundle savedInstanceState) {

		final Button login = (Button) findViewById(R.id.btnLogin);
		user = editLazyLoad(user, R.id.username);
		pass = editLazyLoad(pass, R.id.password);
		
		try {
			
			SharedPreferences preferences = getSharedPreferences("utenteDefault", MODE_PRIVATE);
			user.setText(preferences.getString("user", ""));
			pass.setText(preferences.getString("pass", ""));
			if(user.getText().toString() != null && !user.getText().toString().equals("")){
				utenteDefault = (CheckBox) lazyLoad(utenteDefault, R.id.checkBoxLogin);
				utenteDefault.setChecked(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		login.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(final View v) {
				final WrapUtenti wrap = new WrapUtenti();
				user = editLazyLoad(user, R.id.username);
				final EditText pass = (EditText) findViewById(R.id.password);
				final Utenti utente = wrap.selectByUserAndPass(user.getText().toString(), pass.getText().toString());
				if (utente != null) {
					Controllore.setUtenteLogin(utente);
				}else{
					Toast.makeText(getApplication(), "Utente non esistente: correggi o registra", Toast.LENGTH_SHORT).show();
				}
				final Intent i = new Intent(getApplicationContext(), GSAActivity.class);
				startActivity(i);
			}
		});
		final Button register = (Button) findViewById(R.id.btnRegister);
		register.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(final View v) {
				final Intent i = new Intent(getApplicationContext(), Registrazione.class);
				startActivity(i);
			}
		});
		
		utenteDefault = (CheckBox) lazyLoad(utenteDefault, R.id.checkBoxLogin);
		utenteDefault.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(((CheckBox)v).isChecked()){
					user = editLazyLoad(user, R.id.username);
					pass = editLazyLoad(pass, R.id.password);
					try {
						Editor edit = getFileXmlForConfig("utenteDefault");
						edit.clear();
						edit.putString("user", user.getText().toString());
						edit.putString("pass", pass.getText().toString());
						edit.commit();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});

	}
}
